#pragma once
#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>

struct Transform
{
public:
	glm::vec3 GetRightAxis()
	{
		UpdateMatrix();
		return mInverseMatrix * glm::vec4(1, 0, 0, 1);
	}

	glm::vec3 GetUpAxis()
	{
		UpdateMatrix();
		return mInverseMatrix * glm::vec4(0, 1, 0, 1);
	}

	glm::vec3 GetFrontAxis()
	{
		UpdateMatrix();
		return mInverseMatrix * glm::vec4(0, 0, 1, 1);
	}

	glm::mat4 GetMatrix()
	{
		UpdateMatrix();
		return mMatrix;
	}

	glm::mat4 GetInverseMatrix()
	{
		UpdateMatrix();
		return mInverseMatrix;
	}

	void SetPosition(glm::vec3 position)
	{
		if (mPosition != position)
		{
			isDirty = true;
			mPosition = position;
		}
	}
	void SetRotation(glm::vec3 rotation)
	{
		if (mRotation != rotation)
		{
			isDirty = true;
			mRotation = rotation;
		}
	}

	void SetScale(glm::vec3 scale) 
	{
		if (mScale != scale)
		{
			isDirty = true;
			mScale = scale;
		}
	}

	glm::vec3 GetPosition() { return mPosition; }
	glm::vec3 GetRotation() { return mRotation; }
	glm::vec3 GetScale() { return mScale; }
public:
	static glm::mat4x4 TRS(glm::vec3 translate, glm::quat rotation, glm::vec3 scale);
private:
	void UpdateMatrix();
private:
	glm::mat4x4 mMatrix;
	glm::mat4x4 mInverseMatrix;

	// 全局坐标
	glm::vec3 mPosition;
	glm::vec3 mRotation;
	glm::vec3 mScale = glm::vec3(1.0f, 1.0f, 1.0f);

	// 局部坐标，暂时不实现
	glm::vec3 mPositionLcl;
	glm::vec3 mRotationLcl;
	glm::vec3 mScaleLcl = glm::vec3(1.0f, 1.0f, 1.0f);

	bool isDirty = false;
};